package com.zijie.pay.controller;



import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;

import com.alipay.api.FileItem;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class Pay {
    public static void disableSslVerification() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
                }
            };
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        // 设置商户订单号
        model.setOutTradeNo("70501111111S001111119");

        // 设置订单总金额
        model.setTotalAmount("9.00");

        // 设置订单标题
        model.setSubject("大乐透");

        // 设置产品码
        model.setProductCode("QUICK_WAP_WAY");

        // 设置针对用户授权接口
        model.setAuthToken("appopenBb64d181d0146481ab6a762c00714cC27");

        // 设置用户付款中途退出返回商户网站的地址
        model.setQuitUrl("http://www.taobao.com/product/113714.html");

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setAlipayGoodsId("20010001");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setBody("特价手机");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

        // 设置订单绝对超时时间
        model.setTimeExpire("2016-12-31 10:05:00");

        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        extendParams.setHbFqSellerPercent("100");
        extendParams.setHbFqNum("3");
        extendParams.setIndustryRefluxInfo("{\"scene_code\":\"metro_tradeorder\",\"channel\":\"xxxx\",\"scene_data\":{\"asset_name\":\"ALIPAY\"}}");

        extendParams.setCardType("S0JP0000");
        model.setExtendParams(extendParams);

        // 设置商户传入业务信息
        model.setBusinessParams("{\"mc_create_trade_ip\":\"127.0.0.1\"}");

        // 设置公用回传参数
        model.setPassbackParams("merchantBizType%3d3C%26merchantBizNo%3d2016010101111");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");



        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }




    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCfAg35HaqXDJJD9lZgPxVN4g6RyOVeUTFQkXtH/PEG0YF+Yk01ceOsvcTfZiqRDYikfaNQdpDXB62vha3M/yWTWIQ/FSLM7mU4ErdV6bKbyqPo138s7R3XABg2P7543X81O3eYkykUYgcd74MQqW+rNnBUuUQlfe4PCtoVk1xgBzeec/5mIudPbR0WGERaq6F2y3FvMfNip/KaCzVVu3+K33GSuf4ZiWtysWnCszigonzUtwGBb88zUpJcrZ/4zhUS3cEwTvVBeXkCVw7MRwIi7+LGDGfjum3z9/N6Gy1B/Kt44BBgIcqOy7Lak1vvH9sMSoCSksKUkLHvzVSOkpRHAgMBAAECggEAZMkkmjZXno51V2aYsUDK/CbBHV69I99K5sqP3b1R+QRqk/UzAyFBMojGdOOJL4/ogHqyPZopjGJtkMemtj0zUAXXLTT0bfjdZ4FVe7s5Xs4+vqN5FLznIRQKRLh6vpmGu/MJh0gfza+k2zCpXrJG+brz0H/t1eXQMvCp8usIrkRjXcCl1OsQBso6OIYlWZPG/bk3/Jvnpi2ZufIMGYUN1nuyXrEcMlbRoDSa7X9uk2iubruqcIft49NYb0U75HFgmoTB7Q18ybjUX1zA+dKCuBcWxwvsTPNACQZLu2eZkGaulajflBbnWxCbu0rvBI51qDt6s8RSy4eEJGccw3fv4QKBgQDifgB1Vo/CvDj3JfRJ5lN81Ab7EUdK5jWXw5WIe5A2v/1h0LZefjCqH0hR1S58qnSn/kNXhN1pdWH6ABg56CcBKca+CjQTb99YiSJu1Yptb1PZnt+/+atjggzsecBY48v57iEMclQgnhEB2hWXbYw7qnDk78mN7QVXCBBM9G0CMwKBgQCzuU/LSct25dHb0gPMHMb4YMzbmNVCcWuG8OlZdfS2DF8DDEQXr1g825GNDl0YVfom1JC+sTMIXZ75a8Ie4zBA+RrH+bZYH5zAaFZesmhLxKHV8Jryv8kAXG7ILikNqB3qbypDkluVLBB1nlCXxJGcul1TEXM1GV4+85mfrrnZnQKBgDLBBZ1FvoVm5GUNzkZZJNBOOxo77vvrxoqxu2qQNeru7vriaFuYb5NP20brPLWXBPk4UyLjlTIyqbf6GXOwgD0UfYLMiorogOY4JM0tcQ3A6stTfNRqQGvWEstTJTVGJsllAWbmn8+HXExL8Ja0Z75fHVlQGpyBRrcQCYcVJvBdAoGAXdTaPCCDAchrEIFNSjucBgFD80KP1OiyYvlUW0iiQomi8UxuII+DWgKosyRUpmf8+ljwWR7arftzDHWeG9IxVzq3KMDO2qNmi23t6fLrUOOQOsorm6FVp8KadBjlhiPHuqXfQvgeqHsg/Mdy/Em8XD9QihYKOCbOdb9o7V2j2mECgYEAyrOftbOjhIY+UpppMGgkNrdy5cbnKl6Nh1F+Sd8RWdkgSkSFwSyRy3JObqeAWHVaMWsWFpKdOZ1F0i3lzlWEvHLCaXfMJCu5npkuFDhOwzI7a5Wq/SxcFSgYpmqpPrD6OnKjZvyLM2DDqEN5vJ8V467Qc2AnHS0xWZcRwujPY9s=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhyJzvVv7cIOQgX/Ewb219xZ36QVFcDWLNeNvfE+0tX3u+io9FSv0SZzO1tC23JCrqATLZ7fzHT4ypcrnba791sz9opI6iwLg8NsZC5tD1LDgU2xlNdB8Vl6MGmyVL4ZSaRPOm7wKkhW4Klpm5OC0nfhIIw3HSYDR+3EWpc1Y2a/5CbvJJ8ug9qUvqE89Yd+r53JCFwgF2l5B0UBt9ghTOLiKrK2P+wfeioW5WjTDU5pznujDe6NzpjseagWrWOXsEVNhXyuN7CfHyrxxmtxnqlAlZNfdFOOJ8YlwvJLpDF8C+rT5SVoZ7S87SrY/dVtuVEsGzbvRzNRdDD7CHoOK9QIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000149603050");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

}
